package core.api.offer;

import java.util.Date;

import core.datamodel.Constants;
import core.datamodel.Element;
import core.datamodel.ElementValue;
import core.datamodel.Resource;
import core.datamodel.ResourceCounter;
import jade.util.Logger;

public class OfferHelper {

	private Logger log = Logger.getJADELogger(this.getClass().getName());

	private Resource resource;
	private Date startTime;
	private long interval;

	public OfferHelper (Resource  resource, Date  startTime ,long interval){
		this.resource = resource;
		this.startTime = startTime;
		this.interval = interval;

	}

	public ResourceCounter getDayCounter(String counterName, boolean flat){

		ResourceCounter dayCounter = null;
		// check
		if (resource.getCounters() !=null ){
			for (ResourceCounter counter : resource.getCounters()){
				if (counter.getCounterName().equals(counterName)){
					log.info("Found counter " +counterName + " for resource  "+ resource.getResourceName());
					dayCounter= counter;
					//check if new day reset counter
					if (  ( (long)((startTime.getTime() / Constants.MILLIS_DAY) )*Constants.MILLIS_DAY) !=  ((long)(dayCounter.getLastUpdate().getTime()/ Constants.MILLIS_DAY) )*Constants.MILLIS_DAY ){
						// mezzanotte resetto i counter
						dayCounter.setCounterValue((float) 0);
						dayCounter.setUpdateCounter(true);
						log.info("Reset day counter ");
					}
					break;
				}
			}
		}

		if (dayCounter == null){
			// no counter exist --> create new counter
			log.info("Create new counter ");
			dayCounter = new ResourceCounter();
			dayCounter.setCounterName(counterName);
			dayCounter.setNewCounter(true); // set new counter
			dayCounter.setCounterValue((float) 0);
			// set specific
			dayCounter.setResourceId(resource.getId());
			if (flat){
				// set normaliza date
				long time = (long) ((long) ( startTime.getTime()/ Constants.MILLIS_DAY)* Constants.MILLIS_DAY );
				dayCounter.setLastUpdate( new Date(time)  );

			} else 
				dayCounter.setLastUpdate(startTime);
		}
		return dayCounter;

	}

	public ElementValue getElementValue( Element element, ResourceCounter dayCounter, float MonthlyPrice, boolean flat){
		ElementValue eValue = new ElementValue();
		eValue.setElementId(element.getId());
		eValue.setTimestamp(startTime);

		// calculate charge
		int totValue=1;
		try {
			 totValue = new Integer(element.getValue()).intValue();
		} catch(NumberFormatException ex){
			System.out.println("Value in element is Not Number ...set 1");
			totValue=1;
		}
		if (totValue==0) totValue=1;
		
		System.out.println("TOT VALUE = "+ totValue);
		float charge = 0;
		if (flat){
			// set  flat
			charge = (float)( totValue* MonthlyPrice* Constants.MILLIS_DAY/ Constants.MILLIS_MONTHLY  );
			eValue.setCharge( charge );
			eValue.setTotalDayCharge(charge);
			eValue.setLimitCharge(100);
			
			System.out.println("CHARGE = "+ charge);
		}

		else{
			// set  usage
			charge = (float)( element.getUsage().getAvg()/100*totValue* MonthlyPrice* interval/ Constants.MILLIS_MONTHLY  );
			eValue.setCharge( charge );
			eValue.setTotalDayCharge(dayCounter.getCounterValue() +  charge);
			if (MonthlyPrice!=0)
				eValue.setLimitCharge( eValue.getTotalDayCharge() * 100 / ( totValue*MonthlyPrice / ( Constants.MILLIS_MONTHLY / Constants.MILLIS_DAY )) );
			else
				eValue.setLimitCharge(100);
		}
		
		System.out.println("LIMIT charge = "+ eValue.getLimitCharge());
		//set usage
		if (element.getUsage() != null){
			eValue.setUsage(element.getUsage().getAvg());
		} else{
			eValue.setUsage(0);
		}

		eValue.setTotalDayUsage(100);
		eValue.setLimitUsage(100);

		return eValue;

	}

}
